package com.qiao.glod.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

public abstract class QAdapter<T,V extends View> extends BaseAdapter{

    protected List<T> mData;

    public QAdapter(){
        this.mData = new ArrayList<T>();
    }

    public QAdapter(List<T> list){
        this.mData = list;
    }

    public void setData(List<T> list){
        mData.clear();
        this.mData.addAll(list);
        notifyDataSetChanged();
    }

    public void addData(List<T> list){
        this.mData.addAll(list);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent){
        if(convertView == null){
            convertView = newView(parent);
        }
        bindView((V)convertView,mData.get(position));
        return convertView;
    }

    public abstract V newView(ViewGroup parent);
    public abstract void bindView(V view,T data);

}
